package com.hl.toolkit.admin.system.controller;

import com.tycoin.common.bean.BaseResult;
import com.tycoin.common.controller.AbstractController;
import com.tycoin.common.exception.RestException;
import com.tycoin.common.utils.BeanInfoUtil;
import com.tycoin.system.bean.PasswordBean;
import com.tycoin.system.security.SecurityUtils;
import com.tycoin.system.security.UserAuthorizingRealm;
import com.tycoin.system.slaveentity.User;
import com.tycoin.system.slaverepository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = {"/system/user/profile"})
public class UserProfileController extends AbstractController {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = {""})
    public String profile(Model model) {
        User sessionUser = SecurityUtils.getUser();
        User user = userRepository.findOne(sessionUser.getId());
        model.addAttribute("user", user);
        return "system/user/profile/info";
    }

    @RequestMapping(value = {"edit"}, method = RequestMethod.GET)
    public String toEdit(Model model) {
        User sessionUser = SecurityUtils.getUser();
        User user = userRepository.findOne(sessionUser.getId());
        model.addAttribute("user", user);
        return "/system/user/profile/edit";
    }

    @RequestMapping(value = {"edit"}, method = RequestMethod.POST, consumes = "application/json; charset=UTF-8", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResult edit(@RequestBody User paramUser) {
        User sessionUser = SecurityUtils.getUser();
        User user = userRepository.findOne(sessionUser.getId());
        BeanInfoUtil.bean2bean(paramUser, user, "loginName,name,no,email,phone,mobile");

        beanValidator(user);
        userRepository.save(user);

        return new BaseResult();
    }

    @RequestMapping(value = {"editPassword"}, method = RequestMethod.GET)
    public String toEditPassword(Model model) {
        User sessionUser = SecurityUtils.getUser();
        User user = userRepository.findOne(sessionUser.getId());
        model.addAttribute("user", user);
        return "system/user/profile/editPassword";
    }

    @RequestMapping(value = {"editPassword"}, method = RequestMethod.POST, consumes = "application/json; charset=UTF-8", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResult editPassword(@RequestBody PasswordBean bean) {
        User sessionUser = SecurityUtils.getUser();
        User user = userRepository.findOne(sessionUser.getId());
        if (!UserAuthorizingRealm.validatePassword(bean.getPlainPassword(), user.getPassword())) {
            throw new RestException("请输入正确的原密码!");
        }

        user.setPassword(UserAuthorizingRealm.entryptPassword(bean.getPassword()));

        beanValidator(user);
        userRepository.save(user);
        return new BaseResult();
    }
}